package com.meng.android.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.widget.BaseAdapter;
import android.widget.Filter;

public abstract class AdapterWithImage<T> extends BaseAdapter {

	public interface OnImageClickListener {
		public void onImageClickListener();
	}

	protected Context mContext;
	protected LayoutInflater mInflater;
	protected List<T> mObjects;
	protected List<T> mOriginalValues;
	protected int menuItemResourceId;
	protected OnImageClickListener mCallback;
	protected String keyword = "";
	private final Object mLock = new Object();
	private ArrayFilter mFilter;

	public AdapterWithImage(Context context, List<T> listItem) {
		init(context, -1, -1, listItem);
	}

	public AdapterWithImage(Context context, int menuItemResourceId,
			List<T> listItem) {
		init(context, menuItemResourceId, -1, listItem);
	}

	public AdapterWithImage(Context context, int menuItemResourceId,
			int filterResourceId, List<T> listItem) {
		init(context, menuItemResourceId, filterResourceId, listItem);
	}

	public AdapterWithImage(Context context, int menuItemResourceId,
			List<T> listItem, OnImageClickListener mCallback) {
		init(context, menuItemResourceId, -1, listItem);
		this.mCallback = mCallback;
	}

	public AdapterWithImage(Context context, int menuItemResourceId,
			int filterResourceId, List<T> listItem,
			OnImageClickListener mCallback) {
		init(context, menuItemResourceId, filterResourceId, listItem);
		this.mCallback = mCallback;
	}

	public void setCallbackListener(OnImageClickListener mCallback) {
		this.mCallback = mCallback;
	}

	private void init(Context context, int menuItemResourceId,
			int filterResourceId, List<T> listItem) {
		mContext = context;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.menuItemResourceId = menuItemResourceId;
		this.mObjects = listItem;
	}

	@Override
	public int getCount() {
		return mObjects.size();
	}

	@Override
	public T getItem(int position) {
		return mObjects.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public Filter getFilter() {
		if (mFilter == null) {
			mFilter = new ArrayFilter();
		}
		return mFilter;
	}

	private class ArrayFilter extends Filter {
		@Override
		protected FilterResults performFiltering(CharSequence prefix) {
			FilterResults results = new FilterResults();

			if (mOriginalValues == null) {
				synchronized (mLock) {
					mOriginalValues = new ArrayList<T>(mObjects);
				}
			}

			if (prefix == null || prefix.length() == 0) {
				keyword = "";
				ArrayList<T> list;
				synchronized (mLock) {
					list = new ArrayList<T>(mOriginalValues);
				}
				results.values = list;
				results.count = list.size();
			} else {
				String prefixString = prefix.toString().toLowerCase();
				keyword = prefixString;

				ArrayList<T> values;
				synchronized (mLock) {
					values = new ArrayList<T>(mOriginalValues);
				}

				final int count = values.size();
				final ArrayList<T> newValues = new ArrayList<T>();

				for (int i = 0; i < count; i++) {
					final T value = values.get(i);
					final String valueText = value.toString().toLowerCase();

					// First match against the whole, non-splitted value
					if (valueText.contains(prefixString)) {
						newValues.add(value);
					}
				}

				results.values = newValues;
				results.count = newValues.size();
			}

			return results;
		}

		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			// no inspection unchecked
			mObjects = (List<T>) results.values;
			if (results.count > 0) {
				notifyDataSetChanged();
			} else {
				notifyDataSetInvalidated();
			}
		}
	}

}